home *** CD-ROM | disk | FTP | other *** search
- #!/bin/python
- # -*- coding: iso-8859-1 -*-
-
- """
- Tool.py
-
- David Janes
- BlogMatrix
- 2004.02.12
- """
-
- import os.path
- import cgi
- import xml.sax.saxutils
-
- #
- # Forced inclusions for py2exe
- #
- import Cheetah.Template
- import poplib
- import email
- import email.Utils
-
- class ToolSelected:
- """
- Represents a selected weblog, entry or source
- """
- def __init__(self):
- self.identity = None
-
- self.url = None # use this for Tool actions, unless you know better
-
- self.weblog_url = None
- self.entry_url = None
- self.browser_url = None
-
- self.syndication_url = None
-
- self.weblog_title = None
- self.entry_title = None
-
- class ToolOperations:
- """
- Things you can do to Jaeger. Must be called within 'ToolInterface.invoke_for_*'
- """
- def __init__(self):
- pass
-
- def open_url(self, url, force_new_window = False):
- """
- Open this url in the user's browser
- """
- pass
-
- def subscribe_to(self, url):
- """
- Start the Subscribe to Weblog wizard with this url
- """
-
- def get_userdata(self, key, otherwise):
- """
- Retrieve some user data
- """
- return otherwise
-
- def set_userdata(self, key, value):
- """
- Sets some user data
- """
- pass
-
- def get_weblogs(self):
- """
- Return a list of dictionaries, each dictionary being a weblog.
- There is no particular order to the data.
- """
-
- def log(self, message):
- """
- Log the message to the console
- """
-
- tools = []
-
- class ToolInterface:
- """
- The interface for your tool. Simply create an instance of this object and Jaeger
- will figure it out.
- """
- ON_WEBLOG = 0x01 # weblog only selected
- ON_ENTRY = 0x02 # entry only selected
- ON_SOURCE = 0x04 # item in source pane selected
- IF_BROWSER = 0x08 # IF something is displayed in browser AND 'Use Browser URL' selected
- ON_NOTHING = 0x10 # nothing is selected
- ON_SOMETHING = ON_WEBLOG|ON_ENTRY|ON_SOURCE|IF_BROWSER
- ON_ALL = ON_SOMETHING|ON_NOTHING
-
- WEBSERVER = 0x20 # serves web pages
-
- def __init__(self, capabilities = ON_SOMETHING):
- self.capabilities = capabilities
-
- self.name = str(self.__class__)
- dotx = self.name.rfind('.')
- if dotx > -1: self.name = self.name[dotx + 1:]
- if self.name[:4] == "Tool": self.name = self.name[4:]
-
- tools.append(self)
-
- def get_group(self):
- """
- The name for a tool submenu. Tools with the same
- group name are placed under the same submenu.
- """
- return None
-
- def get_label(self, selected):
- """
- You _must_ define this function in your subclass. It defines the greyed
- out menu item text.
-
- 'selected' _may_ be None during the initialization phase
- """
- raise "ToolInterface.get_label not defined"
-
- def invoke(self, selected, operations):
- """
- This is called to 'do the action' of the tool. This is probably
- where the bulk of your code will go if you're writing something
- reactive to user's code
- """
- return
-
- def pulse(self, operations):
- """
- This function is called periodically, about once a minute. Each
- extension is called sequentially and should avoid hogging the thread
- """
-
- def get_server(self, path):
- """
- For the given 'path', return ( method, page name ), or None
- if the page is not to be served.
-
- 'method' will serve the contents and takes the arguments:
- ( self, operations, path, valuemap)
- and will return
- ( status, content_type, header_map, body_text )
- - status is typically 200 or 404
- - 'body_text' may be a string, or list of strings.
- """
- return None
-
- def serve_webpage(self, operations, path, valuemap):
- result = self.get_server(path)
- if not result:
- return self.serve_filenotfound()
-
- return result[0](operations, path, valuemap)
-
- #
- # --- helper stuff below here ---
- #
- def tool_root(self, full = False):
- port = 5335
- try:
- import BlogHTTPD
- port = BlogHTTPD.standard_port
- except:
- print >> sys.stderr, "Tool.tool_root: problem importing BlogHTTPD?"
-
- # print >> sys.stderr, "Tool.PORT", port
-
- if full:
- return "http://127.0.0.1:%d/tools/%s/" % ( port, self.name )
- else:
- return "/tools/%s/" % self.name
-
- def escape_html(self, text):
- """
- Quote for inclusion in HTML text (i.e. deal with <, >, & and ")
- """
- return cgi.escape(text, True)
-
- def quote_attribute(self, text):
- """
- Quote for inclusion in XML attribute
- (i.e. add surrounding quotes, deal with <, >, & and " and ')
- """
- return xml.sax.saxutils.quoteattr(text)
-
- def text_standard_header(self, path):
- breadcrumbs = '<span style="color: FF2A00">%s</span>' % self.escape_html(self.name)
-
- #
- # build up the path description
- #
- pathlist = []
-
- while path != "/":
- result = self.get_server(path)
- if result:
- pathlist.append(( path, result[1] ))
-
- path, base = os.path.split(path)
-
- result = self.get_server("/")
- if not result or not result[1]: pathlist.append(( "/", self.name + " Extension" ))
- else: pathlist.append(( "/", self.name + " Extension: " + result[1] ))
-
- pathlist.reverse()
-
- breadcrumbs = []
- for path, name in pathlist[:-1]:
- breadcrumbs.append('<a href="%s">%s</a>' %
- ( self.escape_html(self.tool_root() + path[1:]), self.escape_html(name) ))
- breadcrumbs.append('<span style="color: FF2A00">%s</span>' % ( self.escape_html(pathlist[-1][1]) ))
- breadcrumbs = " > ".join(breadcrumbs)
-
- #
- # return the result
- #
- return """\
- <html>
- <head>
- <title>BlogMatrix JΣger</title>
- <link type="text/css" rel="stylesheet" href="/css/style.css">
- <basefont face="Georgia" size=2>
- </head>
- <body bgcolor="#FFFFFF" link="#225588" alink="#993333" vlink="#225588">
- <div class="content" sty>
- <h1>BlogMatrix <span style="color: FF2A00">JΣger</span></h1>
- <div class="breadcrumb2">
- """ + breadcrumbs + """
- </div>
- <div class="rule"></div>
- """
-
- def text_standard_footer(self):
- return """<br clear=all><div class="rule"></div></body></html>"""
-
-
- def text_filenotfound(self):
- return """\
- <html>
- <head>
- <title>BlogMatrix JΣger</title>
- <link type="text/css" rel="stylesheet" href="/css/style.css">
- <basefont face="Georgia" size=2>
- </head>
- <body bgcolor="#FFFFFF" link="#225588" alink="#993333" vlink="#225588">
- <div class="content" sty>
- <h1>BlogMatrix <span style="color: FF2A00">JΣger</span></h1>
- <div class="breadcrumb2">
- <span style="color: FF2A00">""" + self.escape_html(self.name) + """ Extension</span>
- </div>
- <p>
- File not found
- <!--
- -=----------------------------------------------
- -=----------------------------------------------
- -=----------------------------------------------
- -=----------------------------------------------
- -=----------------------------------------------
- -=----------------------------------------------
- -->
- </body>
- </html>
- """
-
- def serve_filenotfound(self):
- return ( 404, "text/html", None, self.text_filenotfound() )
-